// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class turnPageView extends cc.Component {

    @property(cc.Node)
    contentNode : cc.Node = null;

    @property(cc.Node)
    btnList : cc.Node = null;

    initPos = cc.v2(0,0);

    changePage : boolean = false;

    curPage  = 0;

    maxPage = 2;

    type  = 0;

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START,this.TouchStart,this);
        // this.node.on(cc.Node.EventType.TOUCH_MOVE,this.TouchMove,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.TouchEnd,this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.TouchEnd,this);

    }

    TouchStart(event){
        let worldPoint = event.getLocation();
        let pos = this.node.convertToNodeSpaceAR(worldPoint);
        this.initPos = cc.v2(pos.x,pos.y);
    }

    //type 菜单界面   皮肤界面
    setCallBack(curPage,maxPage,type){
        this.curPage = curPage;
        this.maxPage = maxPage;
        this.type = type;
        this.contentNode.y += this.curPage * 500;
        this.updateShowBtn();
    }

    TouchEnd(event){
        let worldPoint = event.getLocation();
        let pos = this.node.convertToNodeSpaceAR(worldPoint);
        let finalPos = cc.v2(pos.x,pos.y);
        let distance = this.initPos.sub(finalPos).mag();
        if(distance > 150 && finalPos.y >= this.initPos.y){
            if(this.curPage >= this.maxPage) return;
            this.changePage = true;
            cc.tween(this.contentNode).by(0.3,{
                y : 500
            }).call(()=>{
                this.curPage++;
                this.updateShowBtn();
                this.changePage = false;
            }).start();
        }else if(distance > 150 && finalPos.y < this.initPos.y){
            if(this.curPage <= 0) return;
            this.changePage = true;
            cc.tween(this.contentNode).by(0.3,{
                y : -500
            }).call(()=>{
                this.curPage--;
                this.updateShowBtn();
                this.changePage = false;
            }).start();
        }
    }

    updateShowBtn(){
        let btnList = this.btnList.children;
        for(let i = 0 ; i < btnList.length ; i++){

            btnList[i].getChildByName("btn1").getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${i + this.curPage * 8 + 1}`);
            btnList[i].getChildByName("btn2").getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${i + this.curPage * 8 + 1}`);
            switch (this.type) {
                case 0 :
                    if(this.curPage * 8 + i >= gameData.eggWallDate.length){
                        btnList[i].active = false;
                    }else{
                        btnList[i].active = true;
                        for(let k in gameData.getPetShopData()){
                            if(parseInt(k) < 15) continue;
                            if(gameData.eggWallDate[this.curPage * 8 + i] && gameData.eggWallDate[this.curPage * 8 + i]["reward"] == gameData.getPetShopData()[parseInt(k)]["psname"]){
                                if(gameData.getPetProcessSkin()[parseInt(k)] == 1){
                                    btnList[i].getChildByName("get").active = true;
                                    btnList[i].getChildByName("btn1").active = false;
                                    btnList[i].getChildByName("btn2").active = false;
                                }else{
                                    let arr = gameData.eggThreadLockData[i + this.curPage * 8];
                                    if(arr.indexOf(0) < 0){
                                        btnList[i].getChildByName("get").active = false;
                                        btnList[i].getChildByName("btn1").active = false;
                                        btnList[i].getChildByName("btn2").active = true;
                                    }else{
                                        btnList[i].getChildByName("get").active = false;
                                        btnList[i].getChildByName("btn1").active = true;
                                        btnList[i].getChildByName("btn2").active = false;
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 1:
                    if(this.curPage * 8 + i >= gameData.skinWallDate.length){
                        btnList[i].active = false;
                    }else{
                        btnList[i].active = true;
                        for(let k in gameData.getRoleShopData()){
                            if(gameData.skinWallDate[this.curPage * 8 + i] && gameData.skinWallDate[this.curPage * 8 + i]["reward"] == gameData.getRoleShopData()[parseInt(k)]["rsname"]){
                                if(gameData.getRoleProcessSkin()[parseInt(k)] == 1){
                                    btnList[i].getChildByName("get").active = true;
                                    btnList[i].getChildByName("btn1").active = false;
                                    btnList[i].getChildByName("btn2").active = false;
                                }else{
                                    let arr = gameData.skinThreadLockData[i + this.curPage * 8];
                                    if(arr.indexOf(0) < 0){
                                        btnList[i].getChildByName("get").active = false;
                                        btnList[i].getChildByName("btn1").active = false;
                                        btnList[i].getChildByName("btn2").active = true;
                                    }else{
                                        btnList[i].getChildByName("get").active = false;
                                        btnList[i].getChildByName("btn1").active = true;
                                        btnList[i].getChildByName("btn2").active = false;
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        }
    }
}
